package nl.hr.cmi.project4.inf1m.groep1.ZuinigRijden;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.text.format.Time;
import android.util.Log;

public class LocationHolder implements LocationListener{
	
	private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
	private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds
	
	private ZuinigRijden parent;
	
	private ArrayList<SmallLocation> locationList;
	private static ArrayList<SmallLocation> openLocationList;
	private boolean recording;
	private Time startTime;
	
	private Stoplicht stoplicht;
	
	private LocationManager locationManager;
	
	public LocationHolder(ZuinigRijden _parent){
		parent = _parent;
		recording = false;
		locationManager = (LocationManager) parent.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                this
                );
        ImageView imageView = (ImageView)parent.findViewById(R.id.stoplichtImage);
        stoplicht = new Stoplicht(imageView ,parent);
	}//
	
	public ArrayList<SmallLocation> getLocationList(){
		return locationList;		
	}
	
	public void clearLocationList(){
		locationList = new ArrayList<SmallLocation>();
	}
	
	public void startRecording(){
		recording = true;
		startTime = new Time();
		startTime.setToNow();
		clearLocationList();
	}
	
	public void stopRecording(){
		recording = false;
		AlertDialog.Builder alert = new AlertDialog.Builder(parent);

		alert.setTitle("Route Name");
		alert.setMessage("Input a name for the route.");

		// Set an EditText view to get user input 
		final EditText input = new EditText(parent);
		Time time = new Time();
		time.setToNow();
		String startMinute = ""+startTime.minute;
		if(startTime.minute < 10){
			startMinute = "0"+startTime.minute;
		}
		String startHour = ""+startTime.hour;
		if(startTime.hour < 10){
			startHour = "0"+startTime.hour;
		}
		String minute = ""+time.minute;
		if(time.minute < 10){
			minute = "0"+time.minute;
		}
		String hour = ""+time.hour;
		if(time.hour < 10){
			hour = "0"+time.hour;
		}
		String routeName = startTime.monthDay+"-"+startTime.month+"-"+startTime.year+"_"+startHour+":"+startMinute+"to"+hour+":"+minute;
		input.setText(routeName);
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			String value = input.getText().toString();
		  	LocationListGPX locationListXML = new LocationListGPX(parent);
		  	locationListXML.saveRoute(locationList, value);
		  	locationList = null;
		  }
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		  public void onClick(DialogInterface dialog, int whichButton) {
		    // Canceled.
		  }
		});

		alert.show();
	}
	
	public void openRoute(){
		final LocationListGPX locationListGPX = new LocationListGPX(parent);
		File[] files = locationListGPX.getRoutes();
		final String[] items = new String[files.length];
		for(int i = 0;i < files.length;i++){
			items[i] = files[i].getName();
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(parent);
		builder.setTitle("Routes");
		builder.setItems(items, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
		    	openLocationList = locationListGPX.getRoute(items[item]);
		    	parent.startActivity(new Intent("nl.hr.cmi.project4.inf1m.groep1.GoogleMapView.CLEARSCREEN"));		    	
		    }
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	public void add(Location location){
		locationList.add(new SmallLocation(location, false));
		stoplicht.bereken(locationList);
	}
	
	public SmallLocation getLastLocation(){
		return locationList.get((locationList.size() -1));
	}
	
	public SmallLocation getLocationAtIndex(int i){
		if(i > (locationList.size() - 1) || (i < 0)){
			return null;
		} else {
			return locationList.get(i);
		}
	}
	
	public boolean isRecording(){
		return recording;
	}

	@Override
	public void onLocationChanged(Location location) {
		parent.updateLocation(location);
		add(location);
	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(parent,
                "GPS turned off.",
                Toast.LENGTH_LONG).show();
	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(parent,
                "GPS turned on.",
                Toast.LENGTH_LONG).show();	
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		String message = "null";
		if(status == LocationProvider.AVAILABLE){
			message = "GPS status changed, GPS available.";
		} else if (status == LocationProvider.OUT_OF_SERVICE){
			message = "GPS status changed, out of service.";
		} else if (status == LocationProvider.TEMPORARILY_UNAVAILABLE){
			message = "GPS status changed, GPS temporarily unavailable.";
		}
		Toast.makeText(parent,
                message,
                Toast.LENGTH_LONG).show();
	}
	public ItemizedOverlayZR getItemizedOverlay(){
		return null;
	}
	
	public static ArrayList<SmallLocation> getOpenLocationList(){
		return openLocationList;
	}
}
